//
//  MessageImageBubbleView.m
//  BaiTengIM
//
//  Created by awh on 2019/6/30.
//  Copyright © 2019 awh. All rights reserved.
//

#import "MessageImageBubbleView.h"
#import "ImageMessageModel.h"

#define kEMMsgImageDefaultSize 120
#define kEMMsgImageMinWidth 50
#define kEMMsgImageMaxWidth 120
#define kEMMsgImageMaxHeight 260

@implementation MessageImageBubbleView

- (instancetype)initWithDirection:(MessageDirection)messageDirection type:(MessageType)messageType {
    self = [super initWithDirection:messageDirection type:messageType];
    if (self) {
        [self configLayerWithBorderWidth:0 andWithCornerRadius:5 andWithBorderColor:[UIColor clearColor]];
        self.contentMode = UIViewContentModeScaleAspectFill;
        CGFloat width = SCREEN_WIDTH * 0.25;
        CGFloat height = width * 1.5;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(height);
        }];
    }
    return self;
}

- (void)setImage:(UIImage *)image width:(CGFloat)width height:(CGFloat)height {
    UIImage *img = image;
    
    __weak typeof(self) weakself = self;
    void (^block)(CGSize aSize) = ^(CGSize aSize) {
        CGSize layoutSize = [weakself getImageSize:CGSizeMake(width, height)];
        [weakself mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(layoutSize.width);
            make.height.mas_equalTo(layoutSize.height);
        }];
    };
    
    CGSize size = CGSizeMake(width, height);
    if (size.width == 0 || size.height == 0) {
        size = CGSizeMake(width, height);
    }
    
    if (img) {
        self.image = img;
        size = img.size;
    } else {
        
    }
    
    block(size);
}

- (CGSize)getImageSize:(CGSize)aSize {
    CGSize retSize = CGSizeMake(kEMMsgImageDefaultSize, kEMMsgImageDefaultSize);
    do {
        if (aSize.width == 0 || aSize.height == 0) {
            break;
        }
        
        NSInteger tmpWidth = aSize.width;
        if (aSize.width < kEMMsgImageMinWidth) {
            tmpWidth = kEMMsgImageMinWidth;
        }
        if (aSize.width > kEMMsgImageMaxWidth) {
            tmpWidth = kEMMsgImageMaxWidth;
        }
        
        NSInteger tmpHeight = tmpWidth / aSize.width * aSize.height;
        if (tmpHeight > kEMMsgImageMaxHeight) {
            tmpHeight = kEMMsgImageMaxHeight;
        }
        
        retSize.width = tmpWidth;
        retSize.height = tmpHeight;
        
    } while (0);
    
    return retSize;
}

- (void)setModel:(BTMessageModel *)model {
    ImageMessageModel *imageModel = (ImageMessageModel *)model;
//    CGFloat width = SCREEN_WIDTH * 0.25;
//    CGFloat height = width * 1.5;
//    [self setImage:image width:width height:height];
    if (imageModel.image) {
        self.image = imageModel.image;
    } else {
//        NSString *fileName = imageModel.path.lastPathComponent;
//        NSString *doucument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//        doucument = [doucument stringByAppendingPathComponent:kMessageImageFileName];
//        NSString *fullpath = [doucument stringByAppendingPathComponent:fileName];
//        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fullpath options:NSDataReadingMappedIfSafe error:nil]];
//        self.image = image;
        [self sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LIFT_APP_USER_PIC_KEY,imageModel.msg]] placeholderImage:[UIImage imageNamed:@"icon_picture"]];
    }

}

@end
